package com.example.test_demo.danli;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 对象地址是一个 说明是单例的  方法里面的局部变量是初始值一直是10  而全局变量  不管是单例对象里面的 还是我这个测试类本身的全局变量 都不能保证安全性
 */
public class TestSingletonThread {
    public int a = 1;

    private TestSingletonThread(){}
    public static final TestSingletonThread t  = new TestSingletonThread();
    public static TestSingletonThread getInstance(){
        return t;
    }
    public void testThread(){
        int b = 10;
        System.out.println(++b);
    }
}
class Test10{
    public static int c = 1;
    private static List<Integer> l = new ArrayList<>();
    private static List<Integer> ll = new ArrayList<>();
    public static void main(String[] args) {
        final CountDownLatch countDownLatch = new CountDownLatch(20000);
        for (int i = 0; i < 20000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        add();
                    } catch (Exception e) {

                    }
                    countDownLatch.countDown();
                }
            }).start();
        }
        try {
            countDownLatch.await();
            //获取list里面的最大值
            System.out.println("获取list里面的最大值 :"+ Collections.max(l));
            System.out.println("单例对象获取list里面的最大值 :"+ Collections.max(ll));
        } catch (InterruptedException e) {

        }

    }
    public static void add(){

        try {
            Thread.sleep(50);

            TestSingletonThread instance = TestSingletonThread.getInstance();
            System.out.println(instance.hashCode());
            instance.testThread();

            System.out.println("a与c对比:"+(++instance.a));
            System.out.println("c  :"+(++c));
            l.add(c);
            ll.add(instance.a);

            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }



    }

}
